package com.esun.payment_api.controller;

import com.esun.common.api.CommonPage;
import com.esun.common.api.CommonResult;
import com.esun.common.utils.BlankUtils;
import com.esun.payment_api.bean.InvContractDetailBean;
import com.esun.payment_api.domain.InvFormContract;
import com.esun.payment_api.domain.SysDictContract;
import com.esun.payment_api.domain.User;
import com.esun.payment_api.dto.PInvInsParam;
import com.esun.payment_api.service.InvFormContractService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.esun.common.utils.JWTUtils.getLoginUser;

@RestController
@RequestMapping("/inv/contract")
public class InvFormContractController {

    @Resource
    InvFormContractService invFormContractService;

    //此接口已弃用，发票-合同：根据invId查看关联所有合同
    @GetMapping("/getListByInvId")
    public CommonResult getListByInvId(Integer invId) {
        try {
            List<InvFormContract> list = invFormContractService.getListByInvId(invId);
            return CommonResult.success(list, "获取合同列表");
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
    }

    //此接口已弃用，发票-合同：根据Id删除相关合同
    @RequestMapping ("/deleteById")
    public CommonResult deleteById(Integer invId, Integer id) {
        try {
            int count = invFormContractService.deleteById(invId, id);
            if (count > 0) {
                return CommonResult.success(count);
            } else {
                return CommonResult.failed("删除失败");
            }
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
    }

    @ApiOperation("发票-合同：根据Id删除相关合同")
    @RequestMapping ("/deleteByIdStr")
    public CommonResult deleteByIdStr(Integer invId, Integer id) {
        PInvInsParam param = new PInvInsParam();
        User user;
        try {
            user = getLoginUser();
        } catch (Exception e) {
            return CommonResult.failed("无法从token中获取登录用户信息");
        }
        if (BlankUtils.isBlank(invId)) {
            return CommonResult.failed("发票id不能为空");
        }
        if (BlankUtils.isBlank(id)) {
            return CommonResult.failed("合同id不能为空");
        }
        param.setIdStr(id.toString());
        param.setInvId(invId);
        param.setCoId((int) user.getCoId());
        param.setUserId((int) user.getId());
        invFormContractService.deleteByIdStr(param);
        return CommonResult.success("删除关联合同成功");
    }

    //此接口已弃用
    @RequestMapping("/insertMultiple2")
    public CommonResult insertMultiple2(@RequestBody Integer invId, List<SysDictContract> records) {
        try {
            int count = invFormContractService.insertMultiple2(invId, records);
            if (count > 0) {
                return CommonResult.success(count);
            }else {
                return CommonResult.failed("新增合同记录失败");
            }
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
    }

    @ApiOperation("发票-合同：新增多条")
    @RequestMapping("/insertMultiple")
    public CommonResult insertMultiple(@RequestBody PInvInsParam param) {
        try {
            System.out.println(" idStr: " + param.getIdStr().replaceAll(",", ":"));
            param.setIdStr(param.getIdStr().replaceAll(",", ":"));
            System.out.println("idStr="+param.getIdStr()+"|invId="+param.getInvId()+"|coId="+param.getCoId()+"|userId="+param.getUserId());
            invFormContractService.insertMultiple(param);
            return CommonResult.success("新增合同记录成功");
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
    }

    @ApiOperation("发票-合同：根据发票id获取可选合同列表")
    @GetMapping("/getContractList")
    public CommonResult getContractList(Short coId, Integer invId, Integer cusId, String searchStr,
                                        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                        @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        if (BlankUtils.isBlank(coId)) {
            return CommonResult.failed("公司ID不能为空");
        }
        if (BlankUtils.isBlank(invId)) {
            return CommonResult.failed("发票ID不能为空");
        }
        List<SysDictContract> list = invFormContractService.getContractList(coId, invId, cusId, searchStr, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(list), "获取合同列表成功(去掉已关联合同)");
    }

    //此接口已弃用，发票-合同：根据invId获取合同详情
    @GetMapping("/getDetailByInvId")
    public CommonResult getDetailByInvId(Short coId, Integer invId) {
        if (BlankUtils.isBlank(coId)) {
            return CommonResult.failed("公司ID不能为空");
        }
        if (BlankUtils.isBlank(invId)) {
            return CommonResult.failed("发票ID不能为空");
        }
        List<InvContractDetailBean> list = invFormContractService.getDetailByInvId(coId, invId);
        return CommonResult.success(list, "获取合同详情成功");
    }

}
